package com.example.androidtest.handler;

import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.androidtest.R;

public class DownloadPic extends AppCompatActivity {
    private static final String TAG = "DownloadPic";

    //线程编号
    private static final int THREAD_1 = 1;
    private static final int THREAD_2 = 2;
    private static final int THREAD_3 = 3;

    private ImageView mImageView1;
    private ImageView mImageView2;
    private ImageView mImageView3;

    //记录消息
    int count = 0;

    ProgressDialog mDialog;

    //图片地址
    String[] urls = new String[]{
            "http://hiphotos.baidu.com/yuangengqiang/pic/item/252196ca222b88ad50664f58.jpg",
            "http://hiphotos.baidu.com/lzc196806/pic/item/d84f738da76514d2f11f3665.jpg",
            "http://hiphotos.baidu.com/735216726/pic/item/2488f146f7ea415e72f05d75.jpg"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_download_pic);
        this.getView();
        System.out.println("DownloadPic---" + Thread.currentThread().getName());

        //设置按钮
        findViewById(R.id.btnStart).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                count = 0;
                mDialog = ProgressDialog.show(DownloadPic.this, "Fac", "Loading. Please wait...", true);
                //开启线程
                new Thread(new LoadImageRunnable(THREAD_1, mHandler, urls[0])).start();
                new Thread(new LoadImageRunnable(THREAD_2, mHandler, urls[1])).start();
                new Thread(new LoadImageRunnable(THREAD_3, mHandler, urls[2])).start();
                DownloadPic.this.setTitle("线程创建完毕");
            }
        });
    }

    private void getView() {
        mImageView1 = (ImageView) findViewById(R.id.img1);
        mImageView2 = (ImageView) findViewById(R.id.img2);
        mImageView3 = (ImageView) findViewById(R.id.img3);
    }

    private Handler mHandler = new Handler() {

        //利用handMessage更新UI
        public void handMessage(Message message) {
            switch (message.what) {
                case DownloadPic.THREAD_1:
                    mImageView1.setImageBitmap((Bitmap) message.obj);
                    Log.i(TAG, "thread-1");
                    break;
                case DownloadPic.THREAD_2:
                    mImageView2.setImageBitmap((Bitmap) message.obj);
                    Log.i(TAG, "thread-1");
                    break;
                case DownloadPic.THREAD_3:
                    mImageView3.setImageBitmap((Bitmap) message.obj);
                    Log.i(TAG, "thread-1");
                    break;

                //如有异常会有提示
                default:
                    String info = "第" + message.what % 10 + "个线程" + "出现异常";
                    Toast.makeText(DownloadPic.this, info, Toast.LENGTH_LONG).show();
                    break;
            }
            count++;
            if (3 == count) {
                mDialog.dismiss();
            }
        }
    };
}
